<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on"
             label-position="left" :class="isMobile?'mobile-container':'desktop-container'">
      <h3 class="title">WatchDog网关中心</h3>
      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon-class="user"/>
        </span>
        <!--auto-complete="on"-->
        <el-input v-model="loginForm.username" name="username" type="text"  placeholder="请输入用户名"/>
      </el-form-item>
      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password"/>
        </span>
        <el-input
          :type="pwdType"
          v-model="loginForm.password"
          name="password"
          auto-complete="on"
          placeholder="请输入密码"
          @keyup.enter.native="handleLogin"/>
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="pwdType === 'password' ? 'eye' : 'eye-open'"/>
        </span>
      </el-form-item>
      <el-form-item>
        <el-button :loading="loading" type="primary" style="width:100%;" @click.native.prevent="handleLogin">
          登录
        </el-button>
      </el-form-item>
      <el-row style="margin-top: -10px">
        <el-col :span="12">
          <el-button type="text" @click="$router.push('/register')">注册账号</el-button>
        </el-col>
        <el-col :span="12">
          <div style="float: right">
            <el-button style="display: block" type="text" @click="$router.push('/reset_password')">忘记密码</el-button>
          </div>
        </el-col>
      </el-row>
      <!--<div class="tips">-->
      <!--<span style="margin-right:20px;">username: admin</span>-->
      <!--<span> password: admin</span>-->
      <!--</div>-->
      <div style="text-align: center;color: white;margin-top: 20px">
        🔥小二科技有限公司@copy2019
      </div>
    </el-form>

  </div>
</template>

<script>
  import {isMobile, isValidPassword, isvalidUsername} from '@/utils/validate'

  export default {
    name: 'Login',
    data() {
      const validateUsername = (rule, value, callback) => {
        // if (!isMobile(value)) {
        //   callback(new Error('请输入正确的手机号'))
        // } else {
        callback()
        // }
      }
      const validatePass = (rule, value, callback) => {
        // if (!isValidPassword(value)) {
        //   callback(new Error('请输入正确的密码,密码由6-16个数字和字母组合'))
        // } else {
        callback()
        // }
      }
      return {
        loginForm: {
          username: 'hushangjie',
          password: '123456'
        },
        loginRules: {
          username: [{required: true, trigger: 'blur', validator: validateUsername}],
          password: [{required: true, trigger: 'blur', validator: validatePass}]
        },
        loading: false,
        pwdType: 'password',
        redirect: undefined,
        isMobile: false, //是否在手机内注册

      }
    },
    watch: {
      $route: {
        handler: function (route) {
          this.redirect = route.query && route.query.redirect
        },
        immediate: true
      }
    },
    methods: {
      checkViewPort() {
        const {body} = document
        const rect = body.getBoundingClientRect()
        this.isMobile = rect.width - 1 < 992
      },
      showPwd() {
        if (this.pwdType === 'password') {
          this.pwdType = ''
        } else {
          this.pwdType = 'password'
        }
      },
      handleLogin() {
        this.$refs.loginForm.validate(valid => {
          if (valid) {
            this.loading = true
            /* loginByMobilePassword(this.loginForm.username, this.loginForm.password).then(res => {
               console.log(res)
               this.$router.push({ path: '/register' })
               this.loading = false
             }).catch(error => {
               console.log(error)
               this.loading = false
             })*/

            this.$store.dispatch('Login', this.loginForm).then(() => {
              this.loading = false
              this.$router.push({path: this.redirect || '/'})
            }).catch(() => {
              this.loading = false
            })
          } else {
            console.log('error submit!!')
            return false
          }
        })
      }
    },
    mounted() {
      this.checkViewPort()

      this.loginForm.username = this.$route.query.mobile || null
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss">
  $bg: #2d3a4b;
  $light_gray: #eee;

  /* reset element-ui css */
  .login-container {
    .el-input {
      display: inline-block;
      height: 47px;
      width: 85%;

      input {
        background: transparent;
        border: 0px;
        -webkit-appearance: none;
        border-radius: 0px;
        padding: 12px 5px 12px 15px;
        color: $light_gray;
        height: 47px;

        &:-webkit-autofill {
          -webkit-box-shadow: 0 0 0px 1000px $bg inset !important;
          -webkit-text-fill-color: #fff !important;
        }
      }
    }

    .el-form-item {
      border: 1px solid rgba(255, 255, 255, 0.1);
      background: rgba(0, 0, 0, 0.1);
      border-radius: 5px;
      color: #454545;
    }

  }

  .mobile-container {
    margin: 80px auto;
  }

  .desktop-container {
    margin: 180px auto;
  }
</style>

<style rel="stylesheet/scss" lang="scss" scoped>
  $bg: #2d3a4b;
  $dark_gray: #889aa4;
  $light_gray: #eee;
  .login-container {
    position: fixed;
    height: 100%;
    width: 100%;
    background: url("../../assets/bg_chahua.jpg") no-repeat center;
    background-size: cover;
    overflow: hidden;
    /*<!--background-color: $bg;-->*/

    .login-form {
      position: absolute;
      left: 0;
      right: 0;
      width: 450px;
      max-width: 95%;
      padding: 20px;
      border-radius: 15px;
      background-color: #2d3a4b;
      opacity: 0.9;
    }

    .tips {
      font-size: 14px;
      color: #fff;
      margin-bottom: 10px;

      span {
        &:first-of-type {
          margin-right: 16px;
        }
      }
    }

    .svg-container {
      padding: 6px 5px 6px 15px;
      color: $dark_gray;
      vertical-align: middle;
      width: 30px;
      display: inline-block;
    }

    .title {
      font-size: 26px;
      font-weight: 400;
      color: $light_gray;
      margin: 0px auto 20px auto;
      text-align: center;
      font-weight: bold;
    }

    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select: none;
    }
  }
</style>
